﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using TreeSharp;
using Styx;
using Styx.Logic;
using Styx.Logic.Pathing;
using Styx.WoWInternals;
using Styx.WoWInternals.WoWObjects;
using Styx.Helpers;

namespace FelMaster.Classes.Hunter
{
    class MarksmanHunter : TalentBase
    {
        public override string Name
        {
            get { return "MarksmanHunter 2.0"; }
        }

        public override string KeySpell
        {
            get { return "Aimed Shot"; }
        }

        public bool HasteCheck()
        {
            return (SC.PlayerHasBuff("Heroism") || SC.PlayerHasBuff("Rapid Fire") || SC.PlayerHasBuff("Bloodlust") || SC.PlayerHasBuff("Time Warp") || SC.PlayerHasBuff("Ancient Hysteria"));
        }

        public override Composite SingleRotation
        {
            get
            {
                return new PrioritySelector(
                    #region AoE
                    new Decorator(
                        ret => Me.GotTarget && SC.CountEnnemiesInRange(Me.CurrentTarget.Location, 12) > 3,
                        new PrioritySelector(
                            new Decorator(x => SC.PlayerIsChanneling, new TreeSharp.Action(a => SC.Log("Channeling"))),
                            SC.UseEngineerGloves(a => true, "Engineer's glove enchant"),
                            SC.CastSpell("Blood Fury", a => true, "Blood Fury"),
                            SC.CastSpell("Lifeblood", a => true, "Lifeblood"),
                            SC.CastSpell("Berserking", a => true, "Berserking"),
                            SC.CastSpell("Trap Launcher", a => SC.CanCast("Explosive Trap"), "Trap Launcher + Explosive Trap"),
                            SC.CastSpell("Multi-Shot", a => true, "Multi-Shot"),
                            SC.CastSpell("Aimed Shot", a => SC.PlayerHasBuff("Fire!"), "Aimed Shot Proc"),
                            new Decorator(
                               ret => SC.PlayerHasBuff("Trap Launcher") && SC.CanCast("Explosive Trap"),
                               new Sequence(
                                   new TreeSharp.Action(ret => SC.CastSpell("Explosive Trap")),
                                   new TreeSharp.Action(ret => Styx.Logic.Combat.LegacySpellManager.ClickRemoteLocation(WoWMathHelper.CalculatePointFrom(Me.Location, Me.CurrentTarget.Location,Math.Max(0, Me.CombatReach+0.33333f + Me.CurrentTarget.CombatReach) - 3f)))
                               )
                            ),
                            SC.CastSpell("Steady Shot", a => true, "Steady Shot")
                        )
                    ),
                    #endregion
                    #region High HP
                    new Decorator(
                        ret => Me.CurrentTarget.HealthPercent >= 90,
                        new PrioritySelector(
                            new Decorator(x => SC.PlayerIsChanneling, new TreeSharp.Action(a => SC.Log("Channeling"))),
                            SC.UseEngineerGloves(a => true, "Engineer's glove enchant"),
                            SC.CastSpell("Blood Fury", a => true, "Blood Fury"),
                            SC.CastSpell("Lifeblood", a => true, "Lifeblood"),
                            SC.CastSpell("Berserking", a => true, "Berserking"),
                            SC.CastSpell("Berserking", a => true, "Berserking"),
                            SC.CastSpell("Serpent Sting", a => !SC.TargetHasDebuff("Serpent Sting"), "Serpent Sting"),
                            SC.CastSpell("Chimera Shot", a => true, "Chimera Shot"),
                            SC.CastSpell("Rapid Fire", a => !HasteCheck(), "Rapid Fire"),
                            SC.CastSpell("Call of the Wild", a => HasteCheck(), "Call of the Wild"),
                            SC.CastSpell("Readiness", a => !SC.CanCast("Rapid Fire") && SC.SpellCooldown("Rapid Fire") > 60, "Readiness"),
                            SC.CastSpell("Aimed Shot", a => SC.PlayerHasBuff("Fire!"), "Aimed Shot Proc"),
                            SC.CastOffensiveBuff("Steady Shot", "Improved Steady Shot", SC.CastTime("Steady Shot") + 1.5, "Steady Shot"),
                            SC.CastSpell("Arcane Shot", a => Me.CurrentFocus > 80 && SC.PlayerHasBuff("Aspect of the Fox"), "Arcane Shot"),
                            SC.CastSpell("Aimed Shot", a => Me.CurrentFocus > 60 || HasteCheck(), "Aimed Shot"),
                            SC.CastSpell("Steady Shot", a => true, "Steady Shot")
                        )
                    ),
                    #endregion
                    #region Low HP
                    new Decorator(
                        ret => Me.CurrentTarget.HealthPercent <= 25,
                        new PrioritySelector(
                            new Decorator(x => SC.PlayerIsChanneling, new TreeSharp.Action(a => SC.Log("Channeling"))),
                            SC.UseEngineerGloves(a => true, "Engineer's glove enchant"),
                            SC.CastSpell("Blood Fury", a => true, "Blood Fury"),
                            SC.CastSpell("Lifeblood", a => true, "Lifeblood"),
                            SC.CastSpell("Berserking", a => true, "Berserking"),
                            SC.CastSpell("Berserking", a => true, "Berserking"),
                            SC.CastSpell("Rapid Fire", a => !HasteCheck(), "Rapid Fire"),
                            SC.CastSpell("Call of the Wild", a => HasteCheck(), "Call of the Wild"),
                            SC.CastSpell("Readiness", a => !SC.CanCast("Rapid Fire") && SC.SpellCooldown("Rapid Fire") > 60, "Readiness"),
                            SC.CastOffensiveBuff("Steady Shot", "Improved Steady Shot", SC.CastTime("Steady Shot") + 1.5, "Steady Shot"),
                            SC.CastSpell("Kill Shot", a => true, "Kill Shot"),
                            SC.CastSpell("Aimed Shot", a => SC.PlayerHasBuff("Fire!"), "Aimed Shot Proc"),
                            SC.CastSpell("Chimera Shot", a => true, "Chimera Shot"),
                            SC.CastSpell("Arcane Shot", a => Me.CurrentFocus > 80 && SC.PlayerHasBuff("Aspect of the Fox"), "Arcane Shot"),
                            SC.CastSpell("Aimed Shot", a => Me.CurrentFocus > 60 || HasteCheck(), "Aimed Shot"),
                            SC.CastSpell("Steady Shot", a => true, "Steady Shot")
                        )
                    ),
                    #endregion
                    #region Standard Rotation
                    new Decorator(
                        ret => Me.CurrentTarget.HealthPercent >= 25 && Me.CurrentTarget.HealthPercent <= 90,
                        new PrioritySelector(
                            new Decorator(x => SC.PlayerIsChanneling, new TreeSharp.Action(a => SC.Log("Channeling"))),
                            SC.UseEngineerGloves(a => true, "Engineer's glove enchant"),
                            SC.CastSpell("Blood Fury", a => true, "Blood Fury"),
                            SC.CastSpell("Lifeblood", a => true, "Lifeblood"),
                            SC.CastSpell("Berserking", a => true, "Berserking"),
                            SC.CastSpell("Berserking", a => true, "Berserking"),
                            SC.CastSpell("Rapid Fire", a => !HasteCheck(), "Rapid Fire"),
                            SC.CastSpell("Call of the Wild", a => HasteCheck(), "Call of the Wild"),
                            SC.CastSpell("Readiness", a => !SC.CanCast("Rapid Fire") && SC.SpellCooldown("Rapid Fire") > 60, "Readiness"),
                            SC.CastSpell("Steady Shot", a => SC.PlayerBuffTimeLeft("Improved Steady Shot") < 1.8, "Steady Shot"),
                            SC.CastSpell("Aimed Shot", a => SC.PlayerHasBuff("Fire!"), "Aimed Shot Proc"),
                            SC.CastSpell("Chimera Shot", a => true, "Chimera Shot"),
                            SC.CastSpell("Arcane Shot", a => Me.CurrentFocus > 80 && SC.PlayerHasBuff("Aspect of the Fox"), "Arcane Shot"),
                            SC.CastSpell("Aimed Shot", a => Me.CurrentFocus > 60 || HasteCheck(), "Aimed Shot"),
                            SC.CastSpell("Serpent Sting", a => !SC.TargetHasDebuff("Serpent Sting"), "Serpent Sting"),
                            SC.CastSpell("Steady Shot", a => true, "Steady Shot")
                        )
                    )
                    #endregion
                );
            }
        }

        public override Composite Buffs
        {
            get
            {
                return new PrioritySelector(
                    SC.CastDebuff("Hunter's Mark", a => Me.CurrentTarget.Level > 85 && Me.CurrentTarget.HealthPercent > 95, "Hunter's Mark")
                );
            }
        }

        public override Composite PVPRotation
        {
            get
            {
                return SingleRotation;
            }
        }

        public override Composite PVERotation
        {
            get
            {
                return SingleRotation;
            }
        }
    }
}
